#include "testApp.h"


//--------------------------------------------------------------
void testApp::setup(){
	string message;
	if( XML.loadFile("data/mySettings2.xml") )
	{

		//read the colors from XML
		//if the settings file doesn't exist we assigns default values (170, 190, 240)
		string ip = XML.getValue("GAME:IP-AEC-BUILDING", "127.0.0.1");
		mySoundManager = new soundManager();
		myBuilding = new building(ip);
		int playersPerTeam = XML.getValue("GAME:PLAYERS-PER-TEAM", 4);

		int readPortGamepads = XML.getValue("GAME:READ-PORT-GAMEPAD", 44000);
		int sendPortGamepads = XML.getValue("GAME:SEND-PORT-GAMEPAD", 55000);
		string sendIPTeamA = XML.getValue("GAME:TEAM-A-SEND-IP-GAMEPAD", "127.0.0.1");
		string sendIPTeamB = XML.getValue("GAME:TEAM-A-SEND-IP-GAMEPAD", "127.0.0.1");	

		myGame = new gamePlay(myBuilding,mySoundManager);
		myPlayersManager = new playersManager(myGame,mySoundManager, playersPerTeam );
		myGamepadsUDP = new gamepadsUDP( myPlayersManager,readPortGamepads,sendIPTeamA,sendIPTeamB,sendPortGamepads  );
		
		int frameRate = XML.getValue("GAME:FRAMERATE", 5);
		ofSetFrameRate(frameRate);
		myGuiGame = new guiGame(myGame,myPlayersManager);

	}else{
		// if XML not loaded then close program
		exit();
	}
	
}

//--------------------------------------------------------------
void testApp::update(){
	
	myGame->cleanColor();
	// render and game mecanics
	ofBackground(100,100,100);
	
	myPlayersManager->update();
	myGame->collisions();
	myGame->update();
	myGame->sendData();
	
	// receive game pads input data
	myGamepadsUDP->update();

}

//--------------------------------------------------------------
void testApp::draw()
{
	
	//----------------------------------------------------------
	// draw all: titles, building display, players display
	myGuiGame->draw();	
	//----------------------------------------------------------
	
	
	//----------------------------------------------------------
	

}


//--------------------------------------------------------------
void testApp::keyPressed(int key){
		switch(key){
		case 'r':
			myGame->resetGame();
			myPlayersManager->initialPosition();
			break;
		case 's':
			if( !myGame->getIsGameStart() ) 
			{
				myGame->startGame();
			}
			break;
		}
}

//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){
}

//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){
}

//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){

}

//--------------------------------------------------------------
void testApp::mouseReleased(){
}
